package com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeCardiacDetection.persenter;

import android.content.Context;
import android.content.Intent;
import android.icu.text.SimpleDateFormat;

import com.example.anew.journey.mynewjourney.R;
import com.example.anew.journey.mynewjourney.base.MyApplication;
import com.example.anew.journey.mynewjourney.bean.ISFirstBean;
import com.example.anew.journey.mynewjourney.bean.TakeDateBean;
import com.example.anew.journey.mynewjourney.config.RBConstants;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeCardiacDetection.bean.ReSultBean;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeCardiacDetection.model.FirstChangeCardiacDetectionModel;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeCardiacDetection.model.ISFirstChangeCardiacDetectionModel;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeCardiacDetection.view.ISFirstChangeCardiacDetectionView;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeHistoricalRecordCardiacDetection.FirstChangeHistoricalRecordCardiacDetectionActvity;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import cn.jesse.nativelogger.NLogger;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 杭州惟君医疗科技有限公司13
 * Created by 20433 on 2018/3/20
 */

public class FirstChangeCardiacDetectionPersenter {
    private ISFirstChangeCardiacDetectionView mISFirstChangeCardiacDetectionView;
    private ISFirstChangeCardiacDetectionModel mISFirstChangeCardiacDetectionModel ;
    private  SimpleDateFormat formatter;

    public FirstChangeCardiacDetectionPersenter(ISFirstChangeCardiacDetectionView iISFirstChangeCardiacDetectionView) {
        this.mISFirstChangeCardiacDetectionView = iISFirstChangeCardiacDetectionView;
        mISFirstChangeCardiacDetectionModel = new FirstChangeCardiacDetectionModel();
        formatter = new SimpleDateFormat("yyyy-MM-dd");
    }
    //开始测试
    public void onClickBtStartTest(){
        String str = mISFirstChangeCardiacDetectionView.getStartTest();
        if(str.equals("开始检测")){
           // mISFirstChangeCardiacDetectionView.toJumpPage();
            mISFirstChangeCardiacDetectionView.toTest();
            mISFirstChangeCardiacDetectionView.SetStartTest(0xff1472FF, R.drawable.cardiac_detection_tvbg_selector_press,"停止");
        }else{
           // mISFirstChangeCardiacDetectionView.toSecondTimeCardiacDetectionPager();
            mISFirstChangeCardiacDetectionView.cancleTest();
            mISFirstChangeCardiacDetectionView.SetStartTest(0xffffffff, R.drawable.cardiac_detection_tvbg_selector_nomale,"开始检测");
        }
    }

    //提示 正在检测中
    public void showIsTesting(){
        mISFirstChangeCardiacDetectionView.showBluetoothTesting();
    }
    //提示 未检测导数据
    public void showNoTestDate(){
        mISFirstChangeCardiacDetectionView.showBluetoothNoTestDate();
    }
    //提示 检测完
    public void showTestOver(){
        mISFirstChangeCardiacDetectionView.showBluetoothTestOver();
    }



    //显示 测试情况
    public  void  showRealTimeTestCase(){
        mISFirstChangeCardiacDetectionView.displayRealTimeTestCase();
    }
    public void hidenRealTimeTestCase(){
        mISFirstChangeCardiacDetectionView.hiddenRealTimeTestCase();
    }


     //设置实时心率

    public void setRealTimeHeartRate(String str){
        mISFirstChangeCardiacDetectionView.setRealTimeHeartRate(str);
        setFetalHeartRateTex();
    }

    //设置 胎儿心率
    public void setFetalHeartRateTex(){
       int  HeartRate =  mISFirstChangeCardiacDetectionView.getRealTimeHeartRate();
        if(HeartRate < 120){
            mISFirstChangeCardiacDetectionView.setFetalHeartRateTextone("过慢",0xffFF9800);
            mISFirstChangeCardiacDetectionView.setFetalHeartRateTextTwo("过快",0xffFF4800);
            mISFirstChangeCardiacDetectionView.setFetalHeartRateTextThread("正常",0xff323232);
        }else if(HeartRate < 180){
            mISFirstChangeCardiacDetectionView.setFetalHeartRateTextone("正常",0xff323232); //"过快",0xffFF4800
            mISFirstChangeCardiacDetectionView.setFetalHeartRateTextTwo("过慢",0xffFF9800);  //"过慢",0xffFF9800
            mISFirstChangeCardiacDetectionView.setFetalHeartRateTextThread("过快",0xffFF4800);
        }else{
            mISFirstChangeCardiacDetectionView.setFetalHeartRateTextone("过快",0xffFF4800); //"过快",0xffFF4800
            mISFirstChangeCardiacDetectionView.setFetalHeartRateTextTwo("正常",0xff323232);  //"过慢",0xffFF9800
            mISFirstChangeCardiacDetectionView.setFetalHeartRateTextThread("过慢",0xffFF9800);  //"正常",0xff323232
        }
    }

    //跳转页面
    public void toumpPage(){
        mISFirstChangeCardiacDetectionView.toJumpPage();
    }


    public void TestReSult(final String str, final Context mContext){
        if (getUserID(mContext) == null) return;
        String url = RBConstants.SERVER_URL + RBConstants.TEST_RESULT;
        OkHttpUtils
                .post()
                .url(url)
                .addParams("cus_id", getUserID(mContext).getUser_id())
                .addParams("result",str)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        final String NetworkResponseDate = response.body().string();
                        Gson gson = new Gson();
                       // NLogger.e("TAG", "===main=========NetworkResponseDate=================" + NetworkResponseDate);
                        final ReSultBean reSultBean =  gson.fromJson(NetworkResponseDate, ReSultBean.class);
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(reSultBean.errorCode == 200){
                                    // todo 记录是否当天第一使用
                                    setRealTimeHeartRate(reSultBean.data.num);
                                    ISFirstBean mbean =   new ISFirstBean();
                                    mbean.setDatestr(str);
                                    mbean.setResult(reSultBean.data.result);
                                    mISFirstChangeCardiacDetectionModel.SaveISFirstDate(mbean,mContext);

                                    Intent intent =   new Intent(mContext,FirstChangeHistoricalRecordCardiacDetectionActvity.class);
                                    NLogger.e("TAG", "===main=========reSultBean.data.num=======123==========" + reSultBean.data.num);
                                    NLogger.e("TAG", "===main=========reSultBean.data.num=======456==========" + reSultBean.data.result);
                                    intent.putExtra("my_test_date",str);
                                    intent.putExtra("my_test_desc",reSultBean.data.result);
                                    mContext.startActivity(intent);







                                    mISFirstChangeCardiacDetectionView.finshPager();
                                }
                            }
                        });
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    private TakeDateBean getUserID(Context mContext) {
        TakeDateBean bean = mISFirstChangeCardiacDetectionModel.TakeDate(mContext);
        if(bean == null) return null;
        return bean;
    }

    //连接搜索中
    public  void search(){
        mISFirstChangeCardiacDetectionView.showSearch();
    }

    public void cancleSearch(){
        mISFirstChangeCardiacDetectionView.cancleSearch();
    }
    public void failureSearch(){
        mISFirstChangeCardiacDetectionView.failureSearch();
    }
    // 连接断开
    public void Disconnect(){
        mISFirstChangeCardiacDetectionView.showDisconnect();
    }

    public void cancleDisconnect(){
        mISFirstChangeCardiacDetectionView.cancleDisconnect();
    }

    public void connectSuccess(){
        mISFirstChangeCardiacDetectionView.connectSuccess();
    }

}
